package com.tmptSource.controller.action;

import com.opensymphony.xwork2.ActionSupport;
import com.tmpt.dto.Result;
import com.tmptSource.dto.A1templatetableDto;
import com.tmptSource.dto.PageDto;
import com.tmptSource.service.intf.IA1templatetableService;
import com.tmpt.utils.*;
import lombok.Getter;
import lombok.Setter;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.math.BigDecimal;
import java.util.*;

@Getter
@Setter
@Controller(value="a1templatetableAction")
@Scope("prototype")
public class A1templatetableAction extends ActionSupport {
	private Logger log = Logger.getLogger(this.getClass());


	//------------
	//与页面交互的分页和排序
	//--------------
	private int page=0;
	private int rows=0;
	private String sortColumn;
	private String sortOrder;

	//------------
	//与页面交互的业务数据
	//--------------
    private String createdate;
	private String createdate_Start;
	private String createdate_End;
    private String createman;
    private String modifydate;
	private String modifydate_Start;
	private String modifydate_End;
    private String modifyman;
    private String id;
    private String keyrandom;
    private String mode;
    private String primarykey;
    private String isdialogAdddialog;
    private String tablename;
    private String tablenameshow;
    private String hasaddbtn;
    private String hasdownloadreportbtn;
    private String haseditbtn;
    private String hashistorybtn;
    private String beanmethod;
    private String beanobject;
    private String beanhistorymethod;
    private String tablealiasname;
    private String foreignkey;
    private String hasdelbtn;
    private String hascompanyid;
    private String sqlprocedure;
    private String height4adddialog;
    private String width4adddialog;
    private String chartjson;
    private String chartheight;
    private String chartwidth;
    private String interceptaddmethod;
    private String intercepteditmethod;
    private String hascheckbox;
    private String datasource;
    private String deleteflag;
    private String interceptobject;
    private String tablenamesql;
    private String tablenamehistorysql;
    private String sqlpage;
    private String sqlcount;
    private String sqlsum;
    private String sqlreport;
    private String sqlpagehistory;
    private String sqlcounthistory;
    private String styleAdddialog;
    private String createmanId;
    private String modifymanId;
    private String companyId;

	//返回结果
	private String result;
	//返回结果
	private List<Map<String,Object>> jsonArrayResult = new ArrayList<Map<String,Object>>();


	@Resource
	private IA1templatetableService a1templatetableService;


	/**
	* 分页查询列表
	* @return
	*/
	public String queryListBySearch(){
		try {

			HttpServletResponse response = ServletActionContext.getResponse();
			HttpServletRequest request = WebUtils.getRequest();
			HttpSession session = request.getSession();

			//查询参数
			//分页/排序查询条件
			PageDto pageDto = new PageDto();
			//默认主键desc
			if(StringUtils.isNull(sortColumn)){
				sortColumn = "id";
				sortOrder = "desc";
			}
			pageDto.setPageCurrent(page-1);
			pageDto.setPageSize(rows);
			pageDto.setSortColumn(sortColumn);
			pageDto.setSortOrder(sortOrder);

			//查询条件
			A1templatetableDto a1templatetableParam = new A1templatetableDto();
			a1templatetableParam.setKeyrandom(this.keyrandom);
			a1templatetableParam.setMode(this.mode);
			a1templatetableParam.setPrimarykey(this.primarykey);
			a1templatetableParam.setTablename(this.tablename!=null?"%"+this.tablename+"%":null);
			a1templatetableParam.setTablenameshow(this.tablenameshow!=null?"%"+this.tablenameshow+"%":null);
			a1templatetableParam.setDeleteflag(this.deleteflag);

			//data limit control
			String requireUrl = request.getRequestURI().toString();
			String queryString = request.getQueryString();
			String contextPath = request.getContextPath();
			//data limit parameters
			String userId = WebUtils.getUserId();
			Map<String,Object> paramMap = new HashMap<String, Object>();
			paramMap.put("userId", userId);
			paramMap.put("contextPath", contextPath);
			paramMap.put("requireUrl", requireUrl);
			paramMap.put("queryString", queryString);

			//调用远程服务
			Result<A1templatetableDto> resultObj =  a1templatetableService.pagination(a1templatetableParam,pageDto,paramMap);
			if(!resultObj.isSuccess()){
				this.result = "error:"+resultObj.getContent();
				JsonPaginationUtils.responseWriter(response, this.result);
				return "jsonResult";
			}
			Map<String,Object> resultMap = resultObj.getResultMap();

			//分页记录
			List<A1templatetableDto> list = (List<A1templatetableDto>)resultMap.get("rows");
			JSONArray jsonArrayRows = JSONArray.fromObject(list);

			//分页总记录数
			int total = 0;
			if(resultMap.get("total") != null){
				total = Integer.parseInt(resultMap.get("total").toString());
			}

			//分页合计（select '合计' as c1, sum(balance) as c2 ....）
			JSONArray jsonArrayFooter = null;
			if(resultMap.get("footer")!=null){
				jsonArrayFooter = JSONArray.fromObject(resultMap.get("footer"));
			}

			JSONObject json = new JSONObject();
			json.put("total", total);
			json.put("rows", jsonArrayRows);
			//json.put("reportSqlString",reportSqlString);
			if(jsonArrayFooter!=null){
				json.put("footer", jsonArrayFooter);
			}

			this.result = json.toString();
			JsonPaginationUtils.responseWriter(response, this.result);

		} catch (Exception e) {
			e.printStackTrace();
		}
		return "jsonResult";
	}

	/**
	* 伪删除数据
	* @return
	*/
	public String removeData(){
		Result resultObj = new Result();
		try {
			HttpServletResponse response = ServletActionContext.getResponse();
			HttpServletRequest request = WebUtils.getRequest();
			HttpSession session = request.getSession();

			//参数
			String adminMan = WebUtils.getUsername();//加入管理员

			//调用远程服务
			resultObj =  a1templatetableService.deleteById(Long.valueOf(id),adminMan);
			if(!resultObj.isSuccess()){
				this.result = "error:"+resultObj.getContent();
				JsonPaginationUtils.responseWriter(response, this.result);
				return "jsonResult";
			}
			this.jsonArrayResult.add(resultObj.getResultMap());

		} catch (Exception e) {
			e.printStackTrace();
			resultObj.fault("error:系统出错:"+e.getLocalizedMessage());
			this.jsonArrayResult.add(resultObj.getResultMap());
		}

		return "jsonArray";
	}


	/**
	* 编辑数据
	* @return
	*/
	public String editData(){
		Result<A1templatetableDto> resultObj = new Result();
		try {
			HttpServletResponse response = ServletActionContext.getResponse();
			HttpServletRequest request = WebUtils.getRequest();
			HttpSession session = request.getSession();

			//参数
			A1templatetableDto a1templatetableDto = new A1templatetableDto();
			String adminMan = WebUtils.getUsername();//加入管理员
            String userId = WebUtils.getUserId();
			a1templatetableDto.setModifymanId(userId);
			a1templatetableDto.setModifyman(adminMan);
			a1templatetableDto.setCompanyId(WebUtils.getCompanyId());
			a1templatetableDto.setId(this.id);
			//编辑数据
			a1templatetableDto.setKeyrandom(this.keyrandom);
			a1templatetableDto.setMode(this.mode);
			a1templatetableDto.setPrimarykey(this.primarykey);
			a1templatetableDto.setIsdialogAdddialog(this.isdialogAdddialog);
			a1templatetableDto.setTablename(this.tablename);
			a1templatetableDto.setTablenameshow(this.tablenameshow);
			a1templatetableDto.setHasaddbtn(this.hasaddbtn);
			a1templatetableDto.setHasdownloadreportbtn(this.hasdownloadreportbtn);
			a1templatetableDto.setHaseditbtn(this.haseditbtn);
			a1templatetableDto.setHashistorybtn(this.hashistorybtn);
			a1templatetableDto.setBeanmethod(this.beanmethod);
			a1templatetableDto.setBeanobject(this.beanobject);
			a1templatetableDto.setBeanhistorymethod(this.beanhistorymethod);
			a1templatetableDto.setTablealiasname(this.tablealiasname);
			a1templatetableDto.setForeignkey(this.foreignkey);
			a1templatetableDto.setHasdelbtn(this.hasdelbtn);
			a1templatetableDto.setHascompanyid(this.hascompanyid);
			a1templatetableDto.setSqlprocedure(this.sqlprocedure);
			a1templatetableDto.setHeight4adddialog(this.height4adddialog);
			a1templatetableDto.setWidth4adddialog(this.width4adddialog);
			a1templatetableDto.setChartjson(this.chartjson);
			a1templatetableDto.setChartheight(this.chartheight);
			a1templatetableDto.setChartwidth(this.chartwidth);
			a1templatetableDto.setInterceptaddmethod(this.interceptaddmethod);
			a1templatetableDto.setIntercepteditmethod(this.intercepteditmethod);
			a1templatetableDto.setHascheckbox(this.hascheckbox);
			a1templatetableDto.setDatasource(this.datasource);
			a1templatetableDto.setInterceptobject(this.interceptobject);
			a1templatetableDto.setTablenamesql(this.tablenamesql);
			a1templatetableDto.setTablenamehistorysql(this.tablenamehistorysql);
			a1templatetableDto.setSqlpage(this.sqlpage);
			a1templatetableDto.setSqlcount(this.sqlcount);
			a1templatetableDto.setSqlsum(this.sqlsum);
			a1templatetableDto.setSqlreport(this.sqlreport);
			a1templatetableDto.setSqlpagehistory(this.sqlpagehistory);
			a1templatetableDto.setSqlcounthistory(this.sqlcounthistory);
			a1templatetableDto.setStyleAdddialog(this.styleAdddialog);

			//调用远程服务
			resultObj =  a1templatetableService.editData(a1templatetableDto);
			if(!resultObj.isSuccess()){
				this.result = "error:"+resultObj.getContent();
				JsonPaginationUtils.responseWriter(response, this.result);
				return "jsonResult";
			}
			this.jsonArrayResult.add(resultObj.getResultMap());

		} catch (Exception e) {
			e.printStackTrace();
			resultObj.fault("error:系统出错:"+e.getLocalizedMessage());
			this.jsonArrayResult.add(resultObj.getResultMap());
		}

		return "jsonArray";
	}

	/**
	* 添加数据
	* @return
	*/
	public String addData(){
		Result<A1templatetableDto> resultObj = new Result();
		try {
			HttpServletResponse response = ServletActionContext.getResponse();
			HttpServletRequest request = WebUtils.getRequest();
			HttpSession session = request.getSession();

			//插入参数
			A1templatetableDto a1templatetableDto = new A1templatetableDto();
			String adminMan = WebUtils.getUsername();//加入管理员
			String userId = WebUtils.getUserId();
			a1templatetableDto.setCreatemanId(userId);
			a1templatetableDto.setCreateman(adminMan);
			a1templatetableDto.setCompanyId(WebUtils.getCompanyId());
			//插入数据
			a1templatetableDto.setKeyrandom(this.keyrandom);
			a1templatetableDto.setMode(this.mode);
			a1templatetableDto.setPrimarykey(this.primarykey);
			a1templatetableDto.setIsdialogAdddialog(this.isdialogAdddialog);
			a1templatetableDto.setTablename(this.tablename);
			a1templatetableDto.setTablenameshow(this.tablenameshow);
			a1templatetableDto.setHasaddbtn(this.hasaddbtn);
			a1templatetableDto.setHasdownloadreportbtn(this.hasdownloadreportbtn);
			a1templatetableDto.setHaseditbtn(this.haseditbtn);
			a1templatetableDto.setHashistorybtn(this.hashistorybtn);
			a1templatetableDto.setBeanmethod(this.beanmethod);
			a1templatetableDto.setBeanobject(this.beanobject);
			a1templatetableDto.setBeanhistorymethod(this.beanhistorymethod);
			a1templatetableDto.setTablealiasname(this.tablealiasname);
			a1templatetableDto.setForeignkey(this.foreignkey);
			a1templatetableDto.setHasdelbtn(this.hasdelbtn);
			a1templatetableDto.setHascompanyid(this.hascompanyid);
			a1templatetableDto.setSqlprocedure(this.sqlprocedure);
			a1templatetableDto.setHeight4adddialog(this.height4adddialog);
			a1templatetableDto.setWidth4adddialog(this.width4adddialog);
			a1templatetableDto.setChartjson(this.chartjson);
			a1templatetableDto.setChartheight(this.chartheight);
			a1templatetableDto.setChartwidth(this.chartwidth);
			a1templatetableDto.setInterceptaddmethod(this.interceptaddmethod);
			a1templatetableDto.setIntercepteditmethod(this.intercepteditmethod);
			a1templatetableDto.setHascheckbox(this.hascheckbox);
			a1templatetableDto.setDatasource(this.datasource);
			a1templatetableDto.setInterceptobject(this.interceptobject);
			a1templatetableDto.setTablenamesql(this.tablenamesql);
			a1templatetableDto.setTablenamehistorysql(this.tablenamehistorysql);
			a1templatetableDto.setSqlpage(this.sqlpage);
			a1templatetableDto.setSqlcount(this.sqlcount);
			a1templatetableDto.setSqlsum(this.sqlsum);
			a1templatetableDto.setSqlreport(this.sqlreport);
			a1templatetableDto.setSqlpagehistory(this.sqlpagehistory);
			a1templatetableDto.setSqlcounthistory(this.sqlcounthistory);
			a1templatetableDto.setStyleAdddialog(this.styleAdddialog);

			//调用远程服务
			resultObj =  a1templatetableService.addData(a1templatetableDto);
			if(!resultObj.isSuccess()){
				this.result = "error:"+resultObj.getContent();
				JsonPaginationUtils.responseWriter(response, this.result);
				return "jsonResult";
			}
			this.jsonArrayResult.add(resultObj.getResultMap());

		} catch (Exception e) {
			e.printStackTrace();
			resultObj.fault("error:系统出错:"+e.getLocalizedMessage());
			this.jsonArrayResult.add(resultObj.getResultMap());
		}
		return "jsonArray";
	}


	//=====================================================
	// setter getter method
	//=====================================================


	public String getCreatedate(){
	return createdate;
	}
	public void setCreatedate(String createdate){
	this.createdate = createdate;
	}
    public String getCreatedate_Start(){
    	return createdate_Start;
    }
    public void setCreatedate_Start(String createdate_Start){
    	this.createdate_Start = createdate_Start;
    }
    public String getCreatedate_End(){
    	return createdate_End;
    }
    public void setCreatedate_End(String createdate_End){
    	this.createdate_End = createdate_End;
    }
	public String getCreateman(){
	return createman;
	}
	public void setCreateman(String createman){
	this.createman = createman;
	}
	public String getModifydate(){
	return modifydate;
	}
	public void setModifydate(String modifydate){
	this.modifydate = modifydate;
	}
    public String getModifydate_Start(){
    	return modifydate_Start;
    }
    public void setModifydate_Start(String modifydate_Start){
    	this.modifydate_Start = modifydate_Start;
    }
    public String getModifydate_End(){
    	return modifydate_End;
    }
    public void setModifydate_End(String modifydate_End){
    	this.modifydate_End = modifydate_End;
    }
	public String getModifyman(){
	return modifyman;
	}
	public void setModifyman(String modifyman){
	this.modifyman = modifyman;
	}
	public String getId(){
	return id;
	}
	public void setId(String id){
	this.id = id;
	}
	public String getKeyrandom(){
	return keyrandom;
	}
	public void setKeyrandom(String keyrandom){
	this.keyrandom = keyrandom;
	}
	public String getMode(){
	return mode;
	}
	public void setMode(String mode){
	this.mode = mode;
	}
	public String getPrimarykey(){
	return primarykey;
	}
	public void setPrimarykey(String primarykey){
	this.primarykey = primarykey;
	}
	public String getIsdialogAdddialog(){
	return isdialogAdddialog;
	}
	public void setIsdialogAdddialog(String isdialogAdddialog){
	this.isdialogAdddialog = isdialogAdddialog;
	}
	public String getTablename(){
	return tablename;
	}
	public void setTablename(String tablename){
	this.tablename = tablename;
	}
	public String getTablenameshow(){
	return tablenameshow;
	}
	public void setTablenameshow(String tablenameshow){
	this.tablenameshow = tablenameshow;
	}
	public String getHasaddbtn(){
	return hasaddbtn;
	}
	public void setHasaddbtn(String hasaddbtn){
	this.hasaddbtn = hasaddbtn;
	}
	public String getHasdownloadreportbtn(){
	return hasdownloadreportbtn;
	}
	public void setHasdownloadreportbtn(String hasdownloadreportbtn){
	this.hasdownloadreportbtn = hasdownloadreportbtn;
	}
	public String getHaseditbtn(){
	return haseditbtn;
	}
	public void setHaseditbtn(String haseditbtn){
	this.haseditbtn = haseditbtn;
	}
	public String getHashistorybtn(){
	return hashistorybtn;
	}
	public void setHashistorybtn(String hashistorybtn){
	this.hashistorybtn = hashistorybtn;
	}
	public String getBeanmethod(){
	return beanmethod;
	}
	public void setBeanmethod(String beanmethod){
	this.beanmethod = beanmethod;
	}
	public String getBeanobject(){
	return beanobject;
	}
	public void setBeanobject(String beanobject){
	this.beanobject = beanobject;
	}
	public String getBeanhistorymethod(){
	return beanhistorymethod;
	}
	public void setBeanhistorymethod(String beanhistorymethod){
	this.beanhistorymethod = beanhistorymethod;
	}
	public String getTablealiasname(){
	return tablealiasname;
	}
	public void setTablealiasname(String tablealiasname){
	this.tablealiasname = tablealiasname;
	}
	public String getForeignkey(){
	return foreignkey;
	}
	public void setForeignkey(String foreignkey){
	this.foreignkey = foreignkey;
	}
	public String getHasdelbtn(){
	return hasdelbtn;
	}
	public void setHasdelbtn(String hasdelbtn){
	this.hasdelbtn = hasdelbtn;
	}
	public String getHascompanyid(){
	return hascompanyid;
	}
	public void setHascompanyid(String hascompanyid){
	this.hascompanyid = hascompanyid;
	}
	public String getSqlprocedure(){
	return sqlprocedure;
	}
	public void setSqlprocedure(String sqlprocedure){
	this.sqlprocedure = sqlprocedure;
	}
	public String getHeight4adddialog(){
	return height4adddialog;
	}
	public void setHeight4adddialog(String height4adddialog){
	this.height4adddialog = height4adddialog;
	}
	public String getWidth4adddialog(){
	return width4adddialog;
	}
	public void setWidth4adddialog(String width4adddialog){
	this.width4adddialog = width4adddialog;
	}
	public String getChartjson(){
	return chartjson;
	}
	public void setChartjson(String chartjson){
	this.chartjson = chartjson;
	}
	public String getChartheight(){
	return chartheight;
	}
	public void setChartheight(String chartheight){
	this.chartheight = chartheight;
	}
	public String getChartwidth(){
	return chartwidth;
	}
	public void setChartwidth(String chartwidth){
	this.chartwidth = chartwidth;
	}
	public String getInterceptaddmethod(){
	return interceptaddmethod;
	}
	public void setInterceptaddmethod(String interceptaddmethod){
	this.interceptaddmethod = interceptaddmethod;
	}
	public String getIntercepteditmethod(){
	return intercepteditmethod;
	}
	public void setIntercepteditmethod(String intercepteditmethod){
	this.intercepteditmethod = intercepteditmethod;
	}
	public String getHascheckbox(){
	return hascheckbox;
	}
	public void setHascheckbox(String hascheckbox){
	this.hascheckbox = hascheckbox;
	}
	public String getDatasource(){
	return datasource;
	}
	public void setDatasource(String datasource){
	this.datasource = datasource;
	}
	public String getDeleteflag(){
	return deleteflag;
	}
	public void setDeleteflag(String deleteflag){
	this.deleteflag = deleteflag;
	}
	public String getInterceptobject(){
	return interceptobject;
	}
	public void setInterceptobject(String interceptobject){
	this.interceptobject = interceptobject;
	}
	public String getTablenamesql(){
	return tablenamesql;
	}
	public void setTablenamesql(String tablenamesql){
	this.tablenamesql = tablenamesql;
	}
	public String getTablenamehistorysql(){
	return tablenamehistorysql;
	}
	public void setTablenamehistorysql(String tablenamehistorysql){
	this.tablenamehistorysql = tablenamehistorysql;
	}
	public String getSqlpage(){
	return sqlpage;
	}
	public void setSqlpage(String sqlpage){
	this.sqlpage = sqlpage;
	}
	public String getSqlcount(){
	return sqlcount;
	}
	public void setSqlcount(String sqlcount){
	this.sqlcount = sqlcount;
	}
	public String getSqlsum(){
	return sqlsum;
	}
	public void setSqlsum(String sqlsum){
	this.sqlsum = sqlsum;
	}
	public String getSqlreport(){
	return sqlreport;
	}
	public void setSqlreport(String sqlreport){
	this.sqlreport = sqlreport;
	}
	public String getSqlpagehistory(){
	return sqlpagehistory;
	}
	public void setSqlpagehistory(String sqlpagehistory){
	this.sqlpagehistory = sqlpagehistory;
	}
	public String getSqlcounthistory(){
	return sqlcounthistory;
	}
	public void setSqlcounthistory(String sqlcounthistory){
	this.sqlcounthistory = sqlcounthistory;
	}
	public String getStyleAdddialog(){
	return styleAdddialog;
	}
	public void setStyleAdddialog(String styleAdddialog){
	this.styleAdddialog = styleAdddialog;
	}
	public String getCreatemanId(){
	return createmanId;
	}
	public void setCreatemanId(String createmanId){
	this.createmanId = createmanId;
	}
	public String getModifymanId(){
	return modifymanId;
	}
	public void setModifymanId(String modifymanId){
	this.modifymanId = modifymanId;
	}
	public String getCompanyId(){
	return companyId;
	}
	public void setCompanyId(String companyId){
	this.companyId = companyId;
	}

	public String getSortColumn() {
		return sortColumn;
	}
	public void setSortColumn(String sortColumn) {
		this.sortColumn = sortColumn;
	}
	public String getSortOrder() {
		return sortOrder;
	}
	public void setSortOrder(String sortOrder) {
		this.sortOrder = sortOrder;
	}
	public int getPage() {
		return page;
	}
	public void setPage(int page) {
		this.page = page;
	}
	public int getRows() {
		return rows;
	}
	public void setRows(int rows) {
		this.rows = rows;
	}
	public String getResult() {
		return result;
	}
	public void setResult(String result) {
		this.result = result;
	}
	public List<Map<String, Object>> getJsonArrayResult() {
		return jsonArrayResult;
	}
	public void setJsonArrayResult(List<Map<String, Object>> jsonArrayResult) {
		this.jsonArrayResult = jsonArrayResult;
	}

}
